package com.bruce.cloud.feign.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Created by bruce in 2025/6/17 02:05
 */
@FeignClient(name = "secret-service", path = "/service")
public interface StoreClient {

    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();

    @GetMapping("/queryStores")
    Page<Store> queryStores(@SpringQueryMap Pageable pageable);

    @PostMapping(value = "/stores/{storeId}", consumes = "application/json", params = "mode=upsert")
    Store update(@PathVariable("storeId") Long storeId, Store store);

    @DeleteMapping("/stores/{storeId:\\d+}")
    void delete(@PathVariable Long storeId);

    // json请求
    @PostMapping("/saveStore")
    Store save(@RequestBody Store store);

    // 表单请求
    @PostMapping(path = "/saveStoreForm", consumes = "application/x-www-form-urlencoded")
    Store saveForm(Store store);

    /**
     * 使用Map作为参数时,
     * 如果使用@RequestParam, 会将参数拼接成key=value&key=value..., map中可以拿到请求参数, request.getParameterMap()也可以拿到
     * 如果不使用@RequestParam, 是将参数写到form表单中请求, queryString 是空的, map中拿不到参数, 可以通过request.getParameterMap()获取到
     * 如果使用@RequestBody, 请求报错"status":415,"error":"Unsupported Media Type"
     *
     * @param params
     * @return
     */
    // 表单请求, 参数用Map
    @PostMapping(path = "/requestMap", consumes = "application/x-www-form-urlencoded")
    Store requestMap(@RequestParam Map<String, String> params);

}
